<?php

App::uses('AppModel', 'Model');

class Headphoto extends AppModel {
	
	public $validate = array(
		'entity' => array(
			'rule' => array('isCompositeUnique', array('entity', 'entity_id')),
			'message' => 'Kombinacija polja [Kategorija + Identifikator kategorije] mora biti jedinstvena'
		),
		'entity_id' => array(
			'rule' => array('isCompositeUnique', array('entity', 'entity_id')),
			'message' => 'Kombinacija polja [Kategorija + Identifikator kategorije] mora biti jedinstvena'
		),
	);
	
	public function isCompositeUnique($check, $fields) {
		$conditions = array();
		foreach($fields as $field) {
			$conditions["Headphoto.$field"] = $this->data['Headphoto'][$field];
		}
		if(!empty($this->data['Headphoto']['id'])){
			$conditions['Headphoto.id !='] = $this->data['Headphoto']['id'];
		}
		
		$exists = $this->find("count", array(
			"conditions" => $conditions
		));
		return $exists>0 ? false : true;
	}
	
}

?>